package com.scnb.iterator.v6;

public class LinkedList_<E> implements Collection_<E> {
    Node head = null;
    Node tail = null;

    private int size = 0;

    public void add(E o) {
        Node n = new Node(o);
        n.next = null;
        if (head == null){
            head = n;
            tail = n;
        }
        tail.next = n;
        tail = n;
        size++;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator_ iterator() {
        return new LinkedListIterator();
    }

    private class Node {
        private E o;
        Node next;

        public Node(E o) {
            this.o = o;
        }
    }

    private class LinkedListIterator implements Iterator_ {
        private Node currentNode = head;

        @Override
        public boolean hasNext() {
            if (currentNode == null) {
                return false;
            }
            return true;
        }

        @Override
        public Object next() {
            Object o = currentNode.o;
            currentNode = currentNode.next;
            return o;
        }
    }

}
